public without sharing class IntentProductHandler {

    public static void setOppProjectType(List<IntentProduct__c> newList) {
        Set<Id> oipSet = new Set<Id>();
        //Set<Id> accSet = new Set<Id>();
        //Set<String> ipc1Set = new Set<String>();

        //拿到意向产品的业务机会Id
        for ( IntentProduct__c ip : newList ) {
            oipSet.add(ip.Opportunity__c);
        }
        if(oipSet.size() > 0)OppSetProjectTypeHandler.calcOppSetProjectType(oipSet);
        /*
        Map<Id, Opportunity> accMap = new Map<Id, Opportunity >([select Id, AccountId, Account.Level__c from Opportunity where Id in :oipSet]);
    
        Map<Id, List<IntentProduct__c>> ipMap = new Map<Id, List<IntentProduct__c>>();
        for (IntentProduct__c ip : [select Id, NewCategory1__c, Product__r.Category1__c, Opportunity__c, Opportunity__r.AccountId, Comment__c from IntentProduct__c where Opportunity__c in: oipSet]) {
            List<IntentProduct__c> ipList = new List<IntentProduct__c>();
            if (ipMap.containsKey(ip.Opportunity__c) == true) { 
                ipList = ipMap.get(ip.Opportunity__c);
            }   
            ipList.add(ip);
            ipMap.put(ip.Opportunity__c, ipList);
        }

        List<Opportunity> opList = new List<Opportunity>();
        for (Id oid : oipSet) {
            Opportunity op = new Opportunity(Id = oid);
            String level = String.valueOf(accMap.get(oid).Account.Level__c);
            
            List<IntentProduct__c> ipList = ipMap.get(oid);
            if (ipList != null) {
                for ( IntentProduct__c  ipt : ipList ) {
                    ipc1Set.add(ipt.Product__r.Category1__c);
                    System.debug(ipt.Product__r.Category1__c);
                }
                System.debug(ipc1Set);
            }
            op.ProjectType__c  = IPQPMECPHandler.calcOppProjectType(level, ipc1Set);
            System.debug(op.ProjectType__c);
            opList.add(op);
        }
        update opList;*/
    }
/*
    public static void setfield(List<IntentProduct__c> list_new){
        Set<Id> oppSet = new Set<Id>();
        Map<Id,Id> ipOppMap = new Map<Id,Id>();

        for (IntentProduct__c ip : list_new) {
            if(ip.Opportunity__c != null ){
               oppSet.add(ip.Opportunity__c);
                ipOppMap.put(ip.Id,ip.Opportunity__c); 
            }
            


        }

        Map<Id,Id> ipaccMap = new Map<Id,Id>();
        Set<Id> accSet = new Set<Id>();

        for (Opportunity opp : [select Id,AccountId from Opportunity where Id in:oppSet]) {
            if(opp.AccountId != null )
                {
                    for (Id k  : ipOppMap.keySet()) {
                        if(ipOppMap.get(k)!= opp.Id)continue;
                        ipaccMap.put(k, opp.AccountId);
                    }
                    accSet.add(opp.AccountId);
                }
        }


        Map<Id,Map<String,String>> onUseMachineModelMap = new Map<Id,Map<String,String>>();
        Map<Id,Map<String,String>> onUseMachineSupplierMap = new Map<Id,Map<String,String>>();

        Map<Id,Map<String,String>> onUseRegentBrandMap = new Map<Id,Map<String,String>>();
        Map<Id,Map<String,String>> onUseRegentSupplierMap = new Map<Id,Map<String,String>>();


///在用仪器
        System.debug('==='+ accSet);
         for(Equipment__c e : [select Id ,Account__c,OpponentSpecialTeam__c,Product__r.Name,Supplier__r.Name from Equipment__c where Account__c =:accSet]){
            if(e.Product__r.Name != null)onUseMachineModelMap = genData(onUseMachineModelMap,e.Account__c ,e.Product__r.Name,e.OpponentSpecialTeam__c);
            if(e.Supplier__r.Name != null)onUseMachineSupplierMap = genData(onUseMachineSupplierMap,e.Account__c,e.Supplier__r.Name,e.OpponentSpecialTeam__c);
            System.debug('==='+onUseMachineModelMap);
            System.debug('==='+onUseMachineSupplierMap);

         }
       
////在用试剂    
         for(AssetProject__c ap :[select Id , Instrument__r.OpponentSpecialTeam__c,Account__c,Supplier__r.Name ,Brand__r.Name from AssetProject__c where Account__c =:accSet ]){
            if(ap.Brand__r.Name != null)onUseRegentBrandMap=genData(onUseRegentBrandMap,ap.Account__c,ap.Brand__r.Name,ap.Instrument__r.OpponentSpecialTeam__c);
            if(ap.Supplier__r.Name != null)onUseRegentSupplierMap=genData(onUseRegentSupplierMap,ap.Account__c,ap.Supplier__r.Name,ap.Instrument__r.OpponentSpecialTeam__c);
             System.debug('==='+onUseRegentBrandMap);
            System.debug('==='+onUseRegentSupplierMap);
         }
      
         for (IntentProduct__c ip : list_new) {
            if(ip.SpecialTeam__c != null  && ipaccMap.get(ip.Id) != null ){
                ip.Machine__c = getResForKey(onUseMachineModelMap,ipaccMap.get(ip.Id),ip.SpecialTeam__c );
                ip.ReagentBrand__c = getResForKey(onUseRegentBrandMap,ipaccMap.get(ip.Id),ip.SpecialTeam__c );
                ip.InstrumentSupplier__c = getResForKey(onUseMachineSupplierMap,ipaccMap.get(ip.Id),ip.SpecialTeam__c );
                ip.ReagentsSupplier__c = getResForKey(onUseRegentSupplierMap,ipaccMap.get(ip.Id),ip.SpecialTeam__c);
            }
            
         }
        
    }
    private static Map<Id,Map<String ,String>> genData(Map<Id,Map<String ,String>> origin ,Id key ,String k1 ,String data ){
         Map<String,String> temp = new Map<String ,String>();
        if(origin.containsKey(key)){
            temp.putAll(origin.get(key));
        }
        temp.put(k1,data);

        origin.put(key,temp);
        return origin; 
    }

     private static String getResForKey(Map<Id,Map<String,String>>data ,String acckey ,String key){
        if(data.get(acckey) == null)return '';
        Map<String,String> objMap = data.get(acckey);

        String res = '';

        for(String k : objMap.keySet()){
            String r = objMap.get(k);
            if(r == key ){
                res += k +',';
            }
        }
        if(res.length() > 0)res = res.substring(0,res.length() -1);
        return res;
    }*/
}